package com.haiyi.software.app;

import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.MyTools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class FragmentMe extends Fragment implements OnClickListener {

	private View thisView;

	private ImageView img_userphoto;

	private TextView textview_username, textView_sex;

	private Button btn_mine_info, btn_mine_hy, btn_mine_lw, btn_mine_hb,
			btn_mine_sz;

	private DisplayImageOptions options;

	private ImageLoader imageLoader;

	private MyTools tools;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		super.onCreateView(inflater, container, savedInstanceState);

		thisView = inflater.inflate(R.layout.fragment_me, null);

		initCom();

		initData();

		return thisView;
	}

	@Override
	public void onResume() {
		// TODO 自动生成的方法存根
		super.onResume();
		initData();
	}

	private void initData() {

		UserInfo uinfo = MainApplication.getInstance().getMineUserInfo();

		if (uinfo != null) {

			this.textview_username.setText(uinfo.getUsername());

			if (uinfo.getUsex() != null) {

				this.textView_sex.setBackgroundResource(tools
						.getSexBackground(uinfo.getUsex()));
				this.textView_sex.setGravity(Gravity.CENTER_VERTICAL);
				this.textView_sex.setPadding(40, 2, 5, 0);
			}

			this.textView_sex.setText(uinfo.getAge());

			String str_photo = this.getResources().getString(R.string.URL)
					+ uinfo.getHeadSmall();

			imageLoader.displayImage(str_photo, this.img_userphoto, options);
		}

	}

	private void initCom() {

		tools = MyTools.getInstance();

		// 图片控件
		imageLoader = ImageLoader.getInstance();
		options = new DisplayImageOptions.Builder()
				.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).considerExifParams(true)
				.displayer(new RoundedBitmapDisplayer(0)).build();

		this.img_userphoto = (ImageView) thisView
				.findViewById(R.id.img_userphoto);

		this.textview_username = (TextView) thisView
				.findViewById(R.id.textview_username);
		this.textView_sex = (TextView) thisView.findViewById(R.id.textView_sex);

		this.btn_mine_info = (Button) thisView.findViewById(R.id.btn_mine_info);
		this.btn_mine_hy = (Button) thisView.findViewById(R.id.btn_mine_hy);
		this.btn_mine_lw = (Button) thisView.findViewById(R.id.btn_mine_lw);
		this.btn_mine_hb = (Button) thisView.findViewById(R.id.btn_mine_hb);
		this.btn_mine_sz = (Button) thisView.findViewById(R.id.btn_mine_sz);

		// 事件
		this.btn_mine_info.setOnClickListener(this);
		this.btn_mine_hb.setOnClickListener(this);
		this.btn_mine_hy.setOnClickListener(this);
		this.btn_mine_lw.setOnClickListener(this);
		this.btn_mine_sz.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		Intent intent;

		switch (v.getId()) {
		case R.id.btn_mine_info:

			intent = new Intent(getActivity(), ActivityMine.class);

			startActivity(intent);

			break;
		case R.id.btn_mine_lw:

			intent = new Intent(getActivity(), ActivityMyGiftList.class);

			startActivity(intent);
			break;

		case R.id.btn_mine_hb:

			intent = new Intent(getActivity(), ActivityRecharge.class);

			startActivity(intent);

			break;
		case R.id.btn_mine_hy:
			intent = new Intent(getActivity(), ActivityMyFriend.class);

			startActivity(intent);
			break;

		case R.id.btn_mine_sz:
			intent = new Intent(getActivity(), ActivitySetting.class);

			startActivity(intent);
			break;
		}
	}
}
